const app = getApp();
const db = wx.cloud.database().collection("serve");

Page({
  data: {
    equipmentDetail: {},
    markers: [],
    scale: 16, // 缩放级别
  },
  onLoad: function (options) {
    // 在页面加载时根据传入的设备ID获取设备详细信息
    const equipmentId = options.id; // 假设设备ID存储在页面参数中的 id 字段中
    this.getEquipmentDetail(equipmentId);
    this.mapCtx = wx.createMapContext("myMap");
  },
  getEquipmentDetail: function (id) {
    // 根据设备ID从数据库中获取设备详细信息
    db.doc(id).get().then(res => {
      console.log(res.data);
      const equipmentDetail = res.data;

      // 确保 equipmentDetail.equipment 存在且是数组
      if (equipmentDetail.equipment && Array.isArray(equipmentDetail.equipment)) {
        // 对 equipmentDetail 中的 cityText 进行排序
        equipmentDetail.equipment.sort((a, b) => {
          const order = {
            "服化-现代-男": 1,
            "服化-现代-女": 2,
            "服化-古代-男": 3,
            "服化-古代-女": 4,
            "服化-其他": 5,
            "道具-电器": 6,
            "道具-家具": 6,
            "道具-其他": 6,
          };
          return order[a.cityText] - order[b.cityText];
        });
      } else {
        console.warn("equipmentDetail.equipment is not defined or not an array");
      }

      const marker = {
        id: 1,
        latitude: equipmentDetail.location2[1],
        longitude: equipmentDetail.location2[0],
        title: equipmentDetail.cname,
      };

      const markers = [marker];
      this.setData({
        equipmentDetail,
        markers
      });
    }).catch(err => {
      console.error(err);
    });
  }
});
